package com.taobao.client.pages;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.sdk.RefundHandleRequest;
import com.taobao.client.util.PopBox;

public class RefundNoGetGood extends Composite {

	private Frame f;
	public static final String RESULT_FLAG = "ResultCode";
	public final static String HISTORY_ITEM = "RefundNoGetGood";
	public static RefundNoGetGood refundAgree;
	public RefundHandleRequest input;
	public static RefundNoGetGood getRefundNoGetGood() {
		if (refundAgree == null) {
			refundAgree = new RefundNoGetGood();
		}
		return refundAgree;
	}
	
	/**
	 * Set the trade id and the refund id
	 * 
	 * @param tradeId The trade id.
	 * @param refundId The refund id.
	 */
	public void setIds(String tradeId, String refundId) {
		input.setBiz_order_id(tradeId);
		input.setRefund_id(refundId);
	}
	
	private RefundNoGetGood() {
		String queryString = Window.Location.getQueryString();

		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			// TODO Process the result
		} else
			initialize();
	}

	private void initialize() {
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("退货失败"));

		f = new Frame();
		f.setVisible(false);

		final TextArea auctionRemark = new TextArea();
		final TextArea orderRemark = new TextArea();
		Button argee = new Button("退货失败");
		
		content.add(new Label("商品备注"));
		content.add(auctionRemark);
		content.add(new Label("订单备注"));
		content.add(orderRemark);
		content.add(argee);
		content.add(f);
		
		argee.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent arg0) {
				input.setAuction_remark(auctionRemark.getText());
				input.setOrder_remark(orderRemark.getText());
				
				RPC.getService().getUrl(input.getURLMap(), inputCallBack);				
			}});
		
		initWidget(content);
		
		// Initialize the input parameters
		input = new RefundHandleRequest();
		input.initialize();	
		input.setUser_action("6");
	}

	AsyncCallback<String> inputCallBack = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable arg0) {
			PopBox.error("构造url出错", arg0.getMessage());
		}

		@Override
		public void onSuccess(String arg0) {
//			f.setUrl(arg0);
//			f.setSize(Data.width, Data.height);
//			f.setVisible(true);
//			Window.open(arg0, null, null);
			PopBox.openWindow(arg0);
		}
	};

	/**
	 * Create a head panel
	 * 
	 * @param title the text to be displayed in the head panel
	 * @return a panel that is displayed in the head part always
	 */
	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}
}
